import { _decorator, Collider2D, Component, Contact2DType, Node } from 'cc';
import { UnitPlanetBase } from './UnitPlanetBase';
import { UnitType } from '../Util/Global';
import { UICtrl } from '../Ctrl/UICtrl';
import { GameStickerType } from '../Model/GameStickerMgr';
const { ccclass, property } = _decorator;

@ccclass('UnitGalaxy')
export class UnitGalaxy extends UnitPlanetBase {

    @property({ override: true, displayName: "固定引力范围" })
    alertFieldRadius: number = 10;
    protected start(): void {
        super.start();

        this.unitType = UnitType.UnitGalaxy;

        //星系支点没有碰撞
        // this.collider.off(Contact2DType.BEGIN_CONTACT, this.onColliderEnter, this);
        // this.collider.sensor = true;
    }

    protected lateUpdateLogic(dt: number): void {
        //如果有环绕对象进行绑定环绕
        if (this._forceTrackTarget && this.FixedTrackMove(dt)) {
            //同时只受到小于目标类型的天体引力影响
            this.UpdateSmallerTargetPlanetForce();
        }
        else {
            this.UpdateBiggerTargetPlanetForce(UnitType.UnitSun);
        }

        // if (this.otherPlanetImpactList.length <= 0) {//附近没有其他行星
        //     this.parentUnit = null;
        //     if (this.isGravityInpact) {
        //         this.isGravityInpact = false;
        //         GameEventMgr.Dispatch(GameEvent.OnPlanetNoGravityInpact, this);
        //     }
        // }

        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_GravityAdd);
        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_MoveAdd);
        this.Stand(dt, 0.01, this.minStandSpeed);
        this._RBVFrame_MoveAdd.set(0, 0);
        this._RBVFrame_GravityAdd.set(0, 0);
    }

    onCheckRocheLimitShow(planet?: UnitPlanetBase, add: boolean = true) {
        if (!planet) {
            planet = this.FilterSelfImpactPlanet_One((planet) => {
                return planet.playerCtrl;
            });
            if (planet == null) {
                UICtrl.ClearStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
        }
        if (planet && planet.playerCtrl) {
            if (add && !this.forceTrackTarget) {
                UICtrl.CreateStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
            else {
                UICtrl.ClearStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
        }
    }

    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }

        let planet = otherCollider.node.getComponent(UnitPlanetBase);

        if (planet) {
            planet.AddPlanetGravityImpact(this);
            if (planet.unitType < this.unitType
                && (!planet.forceTrackTarget
                    || planet.forceTrackTarget.unitType > this.unitType)) {
                if (planet.unitType < this.unitType) {
                    this.CheckPlanetOnTrank(planet);//先泊入轨道
                }
                if (!planet.playerCtrl) {
                    planet.SetFixedTrackMoveTarget(this);
                }
                this.onCheckRocheLimitShow(planet, true);
                // planet.target = this;
            }
        }
    }

    protected onTriggerExit(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }

        let other = otherCollider.node.getComponent(UnitPlanetBase);

        //移除自身引力影响
        if (other) {
            other.RemovePlanetGravityImpact(this);
            this.onCheckRocheLimitShow(other, false);
        }
    }
}


